import 'dart:io';

import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_changdian_football/api/api.dart';
import 'package:flutter_changdian_football/api/env/env.dart';
import 'package:flutter_changdian_football/api/http_utils.dart';
import 'package:flutter_changdian_football/pages/home/home_controller.dart';
import 'package:flutter_changdian_football/pages/mine/user_store.dart';
import 'package:flutter_changdian_football/utils/custom_logger.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:mobcommonlib/mobcommonlib.dart';
import 'package:sharesdk_plugin/sharesdk_plugin.dart';
import 'package:mobpush_plugin/mobpush_plugin.dart';

///
/// 全局配置
///
class Global {

  /// Token
  static String accessToken = '';

  /// 分页
  static int pageSize = 15;

  /// init
  static Future<void> init() async {
    /// 运行初始
    WidgetsFlutterBinding.ensureInitialized();
    await SpUtil.getInstance();

    if (!kIsWeb) {
      /// Android 状态栏为透明的沉浸
      if (Platform.isAndroid) {
        SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          statusBarBrightness: Brightness.dark,
          statusBarIconBrightness: Brightness.dark,
        );
        SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
      }

      /// 强制竖屏
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
    }

    // 初始化request类
    HttpUtils.init(
      baseUrl: Api.baseUrl,
      connectTimeout: 60000
    );

    accessToken = SpUtil.getString("Authorization") ?? '';

    Get.put<UserStore>(UserStore());

    Get.put<HomeController>(HomeController());

    Mobcommonlib.submitPolicyGrantResult(true, null);
    MobpushPlugin.updatePrivacyPermissionStatus(true);
    if (Platform.isIOS) {
      MobpushPlugin.setCustomNotification();
      // 开发环境 false, 线上环境 true
      MobpushPlugin.setAPNsForProduction(Env.envConfig.setAPNsForProduction);
    }
    if (await MobpushPlugin.isPushStopped()) {
      MobpushPlugin.restartPush();
    }


    ShareSDKRegister register = ShareSDKRegister();
    register.setupWechat(
      "wx6e288c0523296658", 
      "354e23e48504dfbe01be166089d4615f", 
      "https://391492e38e8eb017d4f6714a1d8bb322.share2dlink.com/"
    );
    register.setupSinaWeibo(
      "1244580769", 
      "594d5ea076adfc852f496be8a649bdc1", 
      "http://www.sharesdk.cn", 
      "https://391492e38e8eb017d4f6714a1d8bb322.share2dlink.com//"
    );
    register.setupQQ(
      "1112165817", 
      "1VxNEDovVX8MuRNv"
    );
    SharesdkPlugin.regist(register);

    bool isPact = SpUtil.getBool('isPact') ?? false;
    if (isPact) {
      setLocationApiKey();
    }

    /// 配置设计稿尺寸
    setDesignWHD(375, 667);

    configLoading();
  }

  static setLocationApiKey() {
    AMapFlutterLocation.setApiKey('94672238a2f03b012f8d7ef224270cf8', '738cfbaf1a9166dab7aeed01c6f39263');
    AMapFlutterLocation.updatePrivacyShow(true, true);
    AMapFlutterLocation.updatePrivacyAgree(true);
  }


  ///
  /// 初始化加载
  ///
  static void configLoading() {
    EasyLoading.instance
      ..indicatorType = EasyLoadingIndicatorType.wave
      ..indicatorSize = 36.w
      ..loadingStyle = EasyLoadingStyle.custom
      ..progressColor = Colors.white
      ..backgroundColor = Colors.black.withOpacity(0.65) //.withOpacity(0.7)
      ..indicatorColor = Colors.white
      ..textColor = Colors.white
      ..progressWidth = 1
      ..lineWidth = 1
      ..maskColor = Colors.black.withOpacity(0.6) //.withOpacity(0.6)
      ..contentPadding = EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w)
      ..userInteractions = true  /// 当loading展示的时候，是否允许用户操作.
      ..boxShadow = [

      ]
      ..dismissOnTap = false;
  }


}